# Trading Log Filter Script - Optimized Version
# Filter trading batch completion logs from large log files
# Uses regex matching for "买入批次 *** 结束" and "卖出批次 *** 结束" patterns
# Uses streaming read for large files

param(
    [Parameter(Mandatory=$true)]
    [string]$InputFile,
    
    [Parameter(Mandatory=$true)]
    [string]$OutputFile,
    
    [Parameter(Mandatory=$false)]
    [string[]]$MatchPatterns = @("买入批次.*?结束", "卖出批次.*?结束"),
    
    [Parameter(Mandatory=$false)]
    [int]$BufferSize = 1024000,  # 1MB buffer
    
    [Parameter(Mandatory=$false)]
    [switch]$CaseSensitive = $false  # Case sensitive matching
)

# Check if input file exists
if (-not (Test-Path $InputFile)) {
    Write-Error "输入文件不存在: $InputFile"
    exit 1
}

# Get file size
$fileSize = (Get-Item $InputFile).Length
$fileSizeMB = [math]::Round($fileSize / 1MB, 2)
Write-Host "正在处理文件: $InputFile ($fileSizeMB MB)"
Write-Host "筛选条件: 匹配以下任一正则表达式的行"
foreach ($pattern in $MatchPatterns) {
    Write-Host "  - '$pattern'"
}
Write-Host "输出文件: $OutputFile"
Write-Host "区分大小写: $CaseSensitive"

# Initialize counters
$totalLines = 0
$matchedLines = 0
$sellCount = 0  # Sell batch counter
$buyCount = 0   # Buy batch counter
$startTime = Get-Date

# Compile regex options
$regexOptions = if ($CaseSensitive) { 
    [System.Text.RegularExpressions.RegexOptions]::None 
} else { 
    [System.Text.RegularExpressions.RegexOptions]::IgnoreCase 
}

# Pre-compile regex patterns for better performance
$compiledRegexes = @()
foreach ($pattern in $MatchPatterns) {
    $compiledRegexes += [System.Text.RegularExpressions.Regex]::new($pattern, $regexOptions)
}

try {
    # Use StreamReader for streaming read
    $reader = New-Object System.IO.StreamReader($InputFile)
    $writer = New-Object System.IO.StreamWriter($OutputFile, $false, [System.Text.Encoding]::UTF8)
    
    Write-Host "开始处理..."
    
    while ($null -ne ($line = $reader.ReadLine())) {
        $totalLines++
        
        # Check if line matches any regex pattern
        $isMatch = $false
        $matchType = ""
        
        foreach ($regex in $compiledRegexes) {
            if ($regex.IsMatch($line)) {
                $isMatch = $true
                # Determine match type
                if ($regex.Pattern.Contains("买入")) { 
                    $buyCount++
                    $matchType = "买入"
                }
                if ($regex.Pattern.Contains("卖出")) { 
                    $sellCount++
                    $matchType = "卖出"
                }
                break
            }
        }
        
        if ($isMatch) {
            # Add timestamp and match type markers
            $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            $writer.WriteLine("[$timestamp][$matchType] $line")
            $matchedLines++
        }
        
        # Show progress every 100k lines
        if ($totalLines % 100000 -eq 0) {
            $currentTime = Get-Date
            $elapsed = $currentTime - $startTime
            Write-Host "已处理: $totalLines 行, 匹配: $matchedLines 行 (卖出: $sellCount, 买入: $buyCount), 耗时: $($elapsed.ToString('hh\:mm\:ss'))"
        }
    }
    
    # Close file streams
    $reader.Close()
    $writer.Close()
    
    # Display final results
    $endTime = Get-Date
    $totalTime = $endTime - $startTime
    
    Write-Host "="*60
    Write-Host "处理完成!"
    Write-Host "总行数: $totalLines"
    Write-Host "匹配行数: $matchedLines"
    Write-Host "  - 卖出批次结束: $sellCount"
    Write-Host "  - 买入批次结束: $buyCount"
    Write-Host "匹配率: $([math]::Round(($matchedLines / $totalLines) * 100, 4))%"
    Write-Host "总耗时: $($totalTime.ToString('hh\:mm\:ss'))"
    Write-Host "平均处理速度: $([math]::Round($totalLines / $totalTime.TotalSeconds, 0)) 行/秒"
    Write-Host "输出文件: $OutputFile"
    
    # Display output file size
    if (Test-Path $OutputFile) {
        $outputSize = (Get-Item $OutputFile).Length
        $outputSizeMB = [math]::Round($outputSize / 1MB, 2)
        Write-Host "输出文件大小: $outputSizeMB MB"
    }
    
    # Display some match examples (first 10 lines)
    if ($matchedLines -gt 0) {
        Write-Host "`n匹配示例（前10行）:"
        $exampleReader = New-Object System.IO.StreamReader($OutputFile)
        $exampleCount = 0
        while ($null -ne ($exampleLine = $exampleReader.ReadLine()) -and $exampleCount -lt 10) {
            Write-Host "  $exampleLine"
            $exampleCount++
        }
        $exampleReader.Close()
        if ($matchedLines -gt 10) {
            Write-Host "  ... 还有 $($matchedLines - 10) 行匹配结果"
        }
    }
}
catch {
    Write-Error "处理过程中发生错误: $($_.Exception.Message)"
    Write-Error "错误详情: $($_.Exception.StackTrace)"
}
finally {
    # Ensure resources are released
    if ($reader) { $reader.Dispose() }
    if ($writer) { $writer.Dispose() }
}

Write-Host "`n使用示例："
Write-Host "# 基本用法（使用默认正则表达式）"
Write-Host ".\LogFilter.ps1 -InputFile 'C:\logs\trading.txt' -OutputFile 'C:\logs\batch_results.txt'"
Write-Host ""
Write-Host "# 自定义正则表达式"
Write-Host ".\LogFilter.ps1 -InputFile 'C:\logs\trading.txt' -OutputFile 'C:\logs\batch_results.txt' -MatchPatterns @('买入批次.*?结束', '卖出批次.*?结束', '平仓批次.*?完成')"
Write-Host ""
Write-Host "# 区分大小写匹配"
Write-Host ".\LogFilter.ps1 -InputFile 'C:\logs\trading.txt' -OutputFile 'C:\logs\batch_results.txt' -CaseSensitive" 